package com.kfc.sales;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;




import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BestSeller extends ListActivity {
	private LayoutInflater mInflater;
	private Vector<RowData> data;
	RowData rd;
	static final String[] kfcproducts = new String[] {
	    "Bucket Meal", "Chicken Bowl",	"Hotshots","Krushers", "Mashed Potato", "One-Piece Chicken", "Spaghetti", "Tiramisu", "Twister", "Zinger"    	};

	static final String[] sales = new String[] {
			"12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345"	};
	
	
	private Integer[] imgid = {
	  R.drawable.bucketmeal,R.drawable.chickenbowl,R.drawable.hotshots,
	  R.drawable.krushers, R.drawable.mashedpotato,R.drawable.onechicken,R.drawable.spag,
	  R.drawable.tiramisu,R.drawable.twister,
	  R.drawable.zinger,	};
	
	TextView bsheader;
	Calendar currentDate = Calendar.getInstance();
	SimpleDateFormat formatter= new SimpleDateFormat("MMM/dd/yyyy");
	String dateNow = formatter.format(currentDate.getTime());
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bestseller);

        bsheader = (TextView) findViewById(R.id.bestsellerheader);
        bsheader.setText("Best Sellers as of: "+dateNow);
		mInflater = (LayoutInflater) getSystemService(
				Activity.LAYOUT_INFLATER_SERVICE);
			
			data = new Vector<RowData>();
			
			for(int i=0;i<kfcproducts.length;i++){
				try {
				 	rd = new RowData(kfcproducts[i],i,sales[i]);
				    } catch (ParseException e) {
				    	e.printStackTrace();
				   }
				   data.add(rd);
			}
					
					CustomAdapter adapter = new CustomAdapter(this, R.layout.lists,
					                                     R.id.branchloc, data);
					
					setListAdapter(adapter);
					getListView().setTextFilterEnabled(true);
        
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){  
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(v.getContext(),ViewProduct.class);
        rd = data.get(position);
        i.putExtra("Prodname", rd.mproductName);
        i.putExtra("Sales", rd.mproductSales);
        i.putExtra("prodimg", ""+imgid[rd.mproductImage]);
        startActivity(i);
		
	}
	
	 public class RowData {
	       protected String mproductName;
	       protected int mproductImage;
	       protected String mproductSales;
	       
	       RowData(String name,int img,String sales)
	       {
	    	   mproductName=name;
	    	   mproductImage = img;
	    	   mproductSales=sales; 
	       }
	
	}
	  
  public class CustomAdapter extends ArrayAdapter<RowData> {
	  public CustomAdapter(Context context, int resource,
	                        int textViewResourceId, List<RowData> objects) {               
	 super(context, resource, textViewResourceId, objects);
	}
	      @Override
	       public View getView(int position, View convertView, ViewGroup parent) {   
	       ViewHolder holder = null;
	       TextView prodname = null;
	       ImageView prodimg=null;
	       TextView prodsales = null;
	       RowData rowData= getItem(position);
	       if(null == convertView){
	            convertView = mInflater.inflate(R.layout.bestsellerlist, null);
	            holder = new ViewHolder(convertView);
	            convertView.setTag(holder);
	        }
	             holder = (ViewHolder) convertView.getTag();
	             prodname = holder.getprodname();
	             prodname.setText("Product Name: "+rowData.mproductName);                                                     
	             prodimg=holder.getprodimg();
	             prodimg.setImageResource(imgid[rowData.mproductImage]);
	             prodsales = holder.getprodsales();
	             prodsales.setText("Product Sales:"+rowData.mproductSales);    
	             return convertView;
	       }
	            private class ViewHolder {
	            private View mRow;
	            private TextView prodName = null;
	            private ImageView prodImg=null; 
	            private TextView prodSale = null;
	            public ViewHolder(View row) {
	            mRow = row;
	 }
	         public TextView getprodname() {
	             if(null == prodName){
	                 prodName = (TextView) mRow.findViewById(R.id.productname);
	                }
	            return prodName;
	         }     
	         public ImageView getprodimg() {
	             if(null == prodImg){
	            	 prodImg = (ImageView) mRow.findViewById(R.id.prodimg);
	                    }
	           return prodImg;
	         }
	        public TextView getprodsales() {
	             if(null == prodSale){
	            	 prodSale = (TextView) mRow.findViewById(R.id.totalproductsales);
	                                      }
	                return prodSale;
	        }
	     }
	   }

}
